﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sripirom.ATS.Core.Domain;
using Sripirom.ATS.Core.UnitOfWork;
using Sripirom.ATS.Repository.NH.SessionStorage;
using NHibernate;
using Sripirom.ATS.Core.Querying;
//using Sripirom.ATS.Core.Authentication;
using NHibernate.Criterion;

namespace Sripirom.ATS.Repository.NH.Repositories
{
    public abstract class LoggerRepository<T, TEntityKey>
        where T: IAggregateRoot    
        {

            private IUnitOfWork _uow;

            public LoggerRepository(IUnitOfWork uow)
            {
                _uow = uow;

            }

            public TEntityKey New(T entity)   
            {
               object key = SessionFactory.GetCurrentSession().Save(entity);
               _uow.Commit();
               return (TEntityKey)key;
            }

            public void Save(T entity)
            {
                SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
                _uow.Commit();
            }

            public T FindBy(TEntityKey id)
            {
                return SessionFactory.GetCurrentSession().Get<T>(id);
            }

            public void Flush()
            {
                SessionFactory.GetCurrentSession().Flush();
            }
            public void Evict(T entity)
            {
                SessionFactory.GetCurrentSession().Evict(entity);
            }
        }
    }
